/**
 * @(#)TimerTaskTest.java
 *
 *
 * @author 
 * @version 1.00 2011/8/12
 */

import java.util.Timer;
import java.util.TimerTask;
import java.util.Date;

public class TimerTaskTest {

static class DefaultTimerTask extends TimerTask {
	@Override
	public void run() {
		System.out.println("Task run at : "+ (new Date(System.currentTimeMillis())).toString() );
	}
}

static class CancelTimerTask extends TimerTask {
	private Timer timer;
	private Object object;
	public CancelTimerTask(Timer tmr, Object obj){
		this.timer = tmr;
		this.object = obj;
	}
	
	@Override
	public void run(){
		timer.cancel();
		synchronized(object){
			object.notify();
		}
		
	}
}


    public TimerTaskTest() {
    }
    
    public static void main (String args[]) throws InterruptedException {
    	DefaultTimerTask defaultTimerTask = new DefaultTimerTask();
    	Timer timer = new Timer();
    	Object object = new Object();
    	
    	long period = 1000;
    	long currentTimeMillis = System.currentTimeMillis();
    	//timer.scheduleAtFixedRate(defaultTimerTask, new Date(System.currentTimeMillis ), period);
    	timer.scheduleAtFixedRate(defaultTimerTask, new Date(currentTimeMillis - 3100  ), period);
    	
    		
    	CancelTimerTask cancelTimerTask = new CancelTimerTask(timer,object);
    	timer.schedule(cancelTimerTask, new Date (currentTimeMillis + 4999));
    	
    	synchronized(object){
    		object.wait();
    	}
    	
    	System.out.println("Main Thread Notified at : "+ (new Date(System.currentTimeMillis())).toString() );
    
    }
    
    
}